'use client';

import { useEffect } from 'react';
import { useRouter, usePathname } from 'next/navigation';
import { ReactNode } from 'react';

type Props = {
  children: ReactNode;
};

export default function RootLayout({ children }: Props) {
  const router = useRouter();
  const pathname = usePathname();
  useEffect(() => {
    const accessToken = localStorage.getItem('accessToken');

    if (pathname !== '/login') {
      if (accessToken) {
        return; // 用户已登录，允许访问页面
      } else {
        router.push('/login'); // 重定向到登录页
      }
    } else {
      if (accessToken) {
        router.push('/profile'); // 如果用户已登录，跳转到 profile
      }
    }
  }, [router, pathname]);
  return (
    <html lang="en">
      <body>
        {children}
      </body>
    </html>
  )
}